package com.yotoodesign.dianba.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import android.os.Environment;
import android.util.Log;

/**
 * 文件下载工具类
 * 
 * @author wanglin
 * 
 */

public class FileUtils {
	private String SDPATH;
	public String getSDPATH() {
		return SDPATH;
	}

	public FileUtils() {
		// 得到当前外包存储设备的目录
		SDPATH = Environment.getExternalStorageDirectory() + "/";
	}

	/**
	 * 在SD卡上创建文件
	 * 
	 * @param fileName
	 * @return
	 * @throws IOException
	 */

	public File createSDFile(String fileName) throws IOException {
		File file = new File(SDPATH + fileName);
		file.createNewFile();
		return file;
	}

	/**
	 * 在SD卡上创建目录
	 * 
	 * @param dirName
	 * @return
	 */

	public File createSDDir(String dirName) {
		File dir = new File(SDPATH + dirName);
		dir.mkdir();
		return dir;
	}

	/**
	 * 判断文件是否存在
	 * 
	 * @param fileName
	 * @return
	 */

	public boolean isFileExist(String fileName) {
		File file = new File(SDPATH + fileName);
		return file.exists();
	}

	/**
	 * 将一个InputStream的数据写入到SD卡中
	 * 
	 * @param path
	 * @param fileName
	 * @param input
	 * @return
	 */

	public File writeDataToSDCrard(String path, String fileName,
	InputStream input) {
		File file = null;
		OutputStream output = null;
		try {
			// 创建目录
			this.createSDDir(path);
			// 创建文件
			file = this.createSDFile(path + fileName);
			// 创建文件输出流
			output = new FileOutputStream(file);
			// 4个字节的输出
			byte[] b = new byte[4 * 1024];
			while ((input.read(b)) != -1) {
				output.write(b);
			}
			// 刷新流
			output.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return file;
	}
	
	public  String ReadTxtFile(String strFilePath)
    {
        String path = strFilePath;
        String content = ""; //文件内容字符串
            //打开文件
            File file = new File(path);
            //如果path是传递过来的参数，可以做一个非目录的判断
            if (file.isDirectory())
            {
                Log.d(Constant.DIANBA, "The File doesn't not exist.");
            }
            else
            {
                try {
                    InputStream instream = new FileInputStream(file); 
                    if (instream != null) 
                    {
                        InputStreamReader inputreader = new InputStreamReader(instream);
                        BufferedReader buffreader = new BufferedReader(inputreader);
                        String line;
                        //分行读取
                        while (( line = buffreader.readLine()) != null) {
                            content += line + "\n";
                        }                
                        instream.close();
                    }
                }
                catch (java.io.FileNotFoundException e) 
                {
                    Log.d(Constant.DIANBA, "The File doesn't not exist.");
                } 
                catch (IOException e) 
                {
                     Log.d(Constant.DIANBA, e.getMessage());
                }
            }
            return content;
    }

}